package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.dto.BusStop;
import pl.edu.pk.mech.kkbus.dto.DepartureTime;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.BusStopService;

@Controller
public class BusStopController extends RestController {

    private static final Logger log = Logger.getLogger(BusStopController.class.getName());
    
    @Autowired
    BusStopService busStopService;
    
    @RequestMapping(value = "/busstops", method = RequestMethod.GET)
    public @ResponseBody List<BusStop> getBusStops() {
        return busStopService.getAllBusStops();
    }
    
    @RequestMapping(value = "/busstop", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody BusStop bs) { 
        log.info("Request for create new BusStop!");
        boolean result = busStopService.create(bs);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/busstop/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody BusStop bs) {
        log.info("Update bus stop " + id);
        boolean result = busStopService.update(id, bs);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/busstop/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        log.info("Delete bus stop " + id);
        boolean result = busStopService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
    
    // DepartureTime
    @RequestMapping(value = "/departuretime", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus addDepartureTime(@RequestBody DepartureTime dt) {
        log.info("Add departure time for bus stop " + dt.getBusStopId());
    	boolean result = busStopService.addDepartureTime(dt);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/departuretime/{dt}/busstop/{bs}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus addDepartureTime(@PathVariable(value = "dt") long dtId,
            @PathVariable(value = "bs") long bsId) {
        log.info("Delete departure time " + dtId + " for bus " + bsId);
        boolean result = busStopService.deleteDepartureTime(dtId, bsId);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
